8 while(cin
>> rows
>> cols
){
9 //g = matriz de entrada
10 long long g
[rows
][cols
];
11 //dp[i][j] = minimo peso para llegar a la casilla (i,j)
12 //empezando en cualquiera de la última columna
13 long long dp
[rows
][cols
];
14 //p[i][j] = Fila en la que estuve antes de llegar a la casilla
15 //(i,j), pero recorriendo la matriz de derecha a izquierda
17 for (int i
=0; i
<rows
; ++i
){
18 for (int j
=0; j
<cols
-1; ++j
){
24 dp
[i
][cols
-1] = g
[i
][cols
-1];
28 for (int j
=cols
-2; j
>=0; --j
){
29 for (int i
=0; i
<rows
; ++i
){
30 int previousRows
[] = {(i
+rows
-1)%rows
, i
, (i
+1)%rows
};
31 sort(previousRows
, previousRows
+ 3);
32 for (int k
=0; k
<3; ++k
){
33 if (dp
[previousRows
[k
]][j
+1] < dp
[i
][j
]){
34 dp
[i
][j
] = dp
[previousRows
[k
]][j
+1];
35 p
[i
][j
] = previousRows
[k
];
42 long long minWeight
= LLONG_MAX
, firstRow
= -1;
43 for (int i
=0; i
<rows
; ++i
){
44 if (dp
[i
][0] < minWeight
){
54 path
.push_back(p
[i
][j
]);
60 for (int i
=0; i
<path
.size(); ++i
){
61 cout
<< " " << path
[i
] + 1;
64 cout
<< minWeight
<< endl
;